/*
 * contrib/btree_gist/btree_time.c
 */
#include "postgres.h"

#include "btree_gist.h"
#include "btree_utils_num.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/timestamp.h"

typedef struct
{
	TimeADT		lower;
	TimeADT		upper;
} timeKEY;

/*
** 时间操作
*/
PG_FUNCTION_INFO_V1(gbt_time_compress);
PG_FUNCTION_INFO_V1(gbt_timetz_compress);
PG_FUNCTION_INFO_V1(gbt_time_fetch);
PG_FUNCTION_INFO_V1(gbt_time_union);
PG_FUNCTION_INFO_V1(gbt_time_picksplit);
PG_FUNCTION_INFO_V1(gbt_time_consistent);
PG_FUNCTION_INFO_V1(gbt_time_distance);
PG_FUNCTION_INFO_V1(gbt_timetz_consistent);
PG_FUNCTION_INFO_V1(gbt_time_penalty);
PG_FUNCTION_INFO_V1(gbt_time_same);


#ifdef USE_FLOAT8_BYVAL
#define TimeADTGetDatumFast(X) TimeADTGetDatum(X)
#else
#define TimeADTGetDatumFast(X) PointerGetDatum(&(X))
#endif


static bool fc_gbt_timegt(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	const TimeADT *fc_aa = (const TimeADT *) fc_a;
	const TimeADT *fc_bb = (const TimeADT *) fc_b;

	return DatumGetBool(DirectFunctionCall2(time_gt,
											TimeADTGetDatumFast(*fc_aa),
											TimeADTGetDatumFast(*fc_bb)));
}

static bool fc_gbt_timege(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	const TimeADT *fc_aa = (const TimeADT *) fc_a;
	const TimeADT *fc_bb = (const TimeADT *) fc_b;

	return DatumGetBool(DirectFunctionCall2(time_ge,
											TimeADTGetDatumFast(*fc_aa),
											TimeADTGetDatumFast(*fc_bb)));
}

static bool fc_gbt_timeeq(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	const TimeADT *fc_aa = (const TimeADT *) fc_a;
	const TimeADT *fc_bb = (const TimeADT *) fc_b;

	return DatumGetBool(DirectFunctionCall2(time_eq,
											TimeADTGetDatumFast(*fc_aa),
											TimeADTGetDatumFast(*fc_bb)));
}

static bool fc_gbt_timele(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	const TimeADT *fc_aa = (const TimeADT *) fc_a;
	const TimeADT *fc_bb = (const TimeADT *) fc_b;

	return DatumGetBool(DirectFunctionCall2(time_le,
											TimeADTGetDatumFast(*fc_aa),
											TimeADTGetDatumFast(*fc_bb)));
}

static bool fc_gbt_timelt(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	const TimeADT *fc_aa = (const TimeADT *) fc_a;
	const TimeADT *fc_bb = (const TimeADT *) fc_b;

	return DatumGetBool(DirectFunctionCall2(time_lt,
											TimeADTGetDatumFast(*fc_aa),
											TimeADTGetDatumFast(*fc_bb)));
}



static int fc_gbt_timekey_cmp(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	timeKEY    *fc_ia = (timeKEY *) (((const Nsrt *) fc_a)->t);
	timeKEY    *fc_ib = (timeKEY *) (((const Nsrt *) fc_b)->t);
	int			fc_res;

	fc_res = DatumGetInt32(DirectFunctionCall2(time_cmp, TimeADTGetDatumFast(fc_ia->lower), TimeADTGetDatumFast(fc_ib->lower)));
	if (fc_res == 0)
		return DatumGetInt32(DirectFunctionCall2(time_cmp, TimeADTGetDatumFast(fc_ia->upper), TimeADTGetDatumFast(fc_ib->upper)));

	return fc_res;
}

static float8 fc_gbt_time_dist(const void *fc_a, const void *fc_b, FmgrInfo *fc_flinfo)
{
	const TimeADT *fc_aa = (const TimeADT *) fc_a;
	const TimeADT *fc_bb = (const TimeADT *) fc_b;
	Interval   *fc_i;

	fc_i = DatumGetIntervalP(DirectFunctionCall2(time_mi_time,
											  TimeADTGetDatumFast(*fc_aa),
											  TimeADTGetDatumFast(*fc_bb)));
	return (float8) Abs(INTERVAL_TO_SEC(fc_i));
}


static const gbtree_ninfo tinfo =
{
	gbt_t_time,
	sizeof(TimeADT),
	16,							/* sizeof(gbtreekey16) */
	fc_gbt_timegt,
	fc_gbt_timege,
	fc_gbt_timeeq,
	fc_gbt_timele,
	fc_gbt_timelt,
	fc_gbt_timekey_cmp,
	fc_gbt_time_dist
};


PG_FUNCTION_INFO_V1(time_dist);
Datum time_dist(PG_FUNCTION_ARGS)
{
	Datum		fc_diff = DirectFunctionCall2(time_mi_time,
										   PG_GETARG_DATUM(0),
										   PG_GETARG_DATUM(1));

	PG_RETURN_INTERVAL_P(abs_interval(DatumGetIntervalP(fc_diff)));
}


/**************************************************
 * 时间操作
 **************************************************/



Datum gbt_time_compress(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);

	PG_RETURN_POINTER(gbt_num_compress(fc_entry, &tinfo));
}


Datum gbt_timetz_compress(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);
	GISTENTRY  *fc_retval;

	if (fc_entry->leafkey)
	{
		timeKEY    *fc_r = (timeKEY *) palloc(sizeof(timeKEY));
		TimeTzADT  *fc_tz = DatumGetTimeTzADTP(fc_entry->key);
		TimeADT		fc_tmp;

		fc_retval = palloc(sizeof(GISTENTRY));

		/* 我们仅使用时间 + 时区来进行压缩 */
		fc_tmp = fc_tz->time + (fc_tz->zone * INT64CONST(1000000));
		fc_r->lower = fc_r->upper = fc_tmp;
		gistentryinit(*fc_retval, PointerGetDatum(fc_r),
					  fc_entry->rel, fc_entry->page,
					  fc_entry->offset, false);
	}
	else
		fc_retval = fc_entry;
	PG_RETURN_POINTER(fc_retval);
}

Datum gbt_time_fetch(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);

	PG_RETURN_POINTER(gbt_num_fetch(fc_entry, &tinfo));
}

Datum gbt_time_consistent(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);
	TimeADT		fc_query = PG_GETARG_TIMEADT(1);
	StrategyNumber fc_strategy = (StrategyNumber) PG_GETARG_UINT16(2);

	/* Oid		subtype = PG_GETARG_OID(3); */
	bool	   *fc_recheck = (bool *) PG_GETARG_POINTER(4);
	timeKEY    *fc_kkk = (timeKEY *) DatumGetPointer(fc_entry->key);
	GBT_NUMKEY_R fc_key;

	/* 由此函数服务的所有案例都是精确的 */
	*fc_recheck = false;

	fc_key.lower = (GBT_NUMKEY *) &fc_kkk->lower;
	fc_key.upper = (GBT_NUMKEY *) &fc_kkk->upper;

	PG_RETURN_BOOL(gbt_num_consistent(&fc_key, (void *) &fc_query, &fc_strategy,
									  GIST_LEAF(fc_entry), &tinfo, fcinfo->flinfo));
}

Datum gbt_time_distance(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);
	TimeADT		fc_query = PG_GETARG_TIMEADT(1);

	/* Oid		subtype = PG_GETARG_OID(3); */
	timeKEY    *fc_kkk = (timeKEY *) DatumGetPointer(fc_entry->key);
	GBT_NUMKEY_R fc_key;

	fc_key.lower = (GBT_NUMKEY *) &fc_kkk->lower;
	fc_key.upper = (GBT_NUMKEY *) &fc_kkk->upper;

	PG_RETURN_FLOAT8(gbt_num_distance(&fc_key, (void *) &fc_query, GIST_LEAF(fc_entry),
									  &tinfo, fcinfo->flinfo));
}

Datum gbt_timetz_consistent(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);
	TimeTzADT  *fc_query = PG_GETARG_TIMETZADT_P(1);
	StrategyNumber fc_strategy = (StrategyNumber) PG_GETARG_UINT16(2);

	/* Oid		subtype = PG_GETARG_OID(3); */
	bool	   *fc_recheck = (bool *) PG_GETARG_POINTER(4);
	timeKEY    *fc_kkk = (timeKEY *) DatumGetPointer(fc_entry->key);
	TimeADT		fc_qqq;
	GBT_NUMKEY_R fc_key;

	/* 此函数处理的所有情况都是不准确的 */
	*fc_recheck = true;

	fc_qqq = fc_query->time + (fc_query->zone * INT64CONST(1000000));

	fc_key.lower = (GBT_NUMKEY *) &fc_kkk->lower;
	fc_key.upper = (GBT_NUMKEY *) &fc_kkk->upper;

	PG_RETURN_BOOL(gbt_num_consistent(&fc_key, (void *) &fc_qqq, &fc_strategy,
									  GIST_LEAF(fc_entry), &tinfo, fcinfo->flinfo));
}


Datum gbt_time_union(PG_FUNCTION_ARGS)
{
	GistEntryVector *fc_entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
	void	   *fc_out = palloc(sizeof(timeKEY));

	*(int *) PG_GETARG_POINTER(1) = sizeof(timeKEY);
	PG_RETURN_POINTER(gbt_num_union((void *) fc_out, fc_entryvec, &tinfo, fcinfo->flinfo));
}


Datum gbt_time_penalty(PG_FUNCTION_ARGS)
{
	timeKEY    *fc_origentry = (timeKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
	timeKEY    *fc_newentry = (timeKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
	float	   *fc_result = (float *) PG_GETARG_POINTER(2);
	Interval   *fc_intr;
	double		fc_res;
	double		fc_res2;

	fc_intr = DatumGetIntervalP(DirectFunctionCall2(time_mi_time,
												 TimeADTGetDatumFast(fc_newentry->upper),
												 TimeADTGetDatumFast(fc_origentry->upper)));
	fc_res = INTERVAL_TO_SEC(fc_intr);
	fc_res = Max(fc_res, 0);

	fc_intr = DatumGetIntervalP(DirectFunctionCall2(time_mi_time,
												 TimeADTGetDatumFast(fc_origentry->lower),
												 TimeADTGetDatumFast(fc_newentry->lower)));
	fc_res2 = INTERVAL_TO_SEC(fc_intr);
	fc_res2 = Max(fc_res2, 0);

	fc_res += fc_res2;

	*fc_result = 0.0;

	if (fc_res > 0)
	{
		fc_intr = DatumGetIntervalP(DirectFunctionCall2(time_mi_time,
													 TimeADTGetDatumFast(fc_origentry->upper),
													 TimeADTGetDatumFast(fc_origentry->lower)));
		*fc_result += FLT_MIN;
		*fc_result += (float) (fc_res / (fc_res + INTERVAL_TO_SEC(fc_intr)));
		*fc_result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
	}

	PG_RETURN_POINTER(fc_result);
}


Datum gbt_time_picksplit(PG_FUNCTION_ARGS)
{
	PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
										(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
										&tinfo, fcinfo->flinfo));
}

Datum gbt_time_same(PG_FUNCTION_ARGS)
{
	timeKEY    *fc_b1 = (timeKEY *) PG_GETARG_POINTER(0);
	timeKEY    *fc_b2 = (timeKEY *) PG_GETARG_POINTER(1);
	bool	   *fc_result = (bool *) PG_GETARG_POINTER(2);

	*fc_result = gbt_num_same((void *) fc_b1, (void *) fc_b2, &tinfo, fcinfo->flinfo);
	PG_RETURN_POINTER(fc_result);
}
